##
# Scons script to install SQLite sources if not present
##

import os
import shutil

Import('env')

target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')
build_dir = env.get('BUILD_DIR')

targets_need_sqlite = ['android', 'msys_nt', 'windows', 'ios']
sqlite_dir = src_dir + '/extlibs/sqlite3/'
sqlite_build_dir = src_dir + '/extlibs/sqlite3/sqlite-amalgamation-3081101/'
sqlite_zip_file = src_dir + '/extlibs/sqlite3/sqlite-amalgamation-3081101.zip'
sqlite_url = 'https://www.sqlite.org/2015/sqlite-amalgamation-3081101.zip'
sqlite_package = 'sqlite-amalgamation-3081101'
sqlite_url = 'https://www.sqlite.org/2015/' + sqlite_package + '.zip'

sqlite_dir = os.path.join(src_dir, 'extlibs', 'sqlite3')
sqlite_c = os.path.join(sqlite_dir, 'sqlite3.c')
sqlite_h = os.path.join(sqlite_dir, 'sqlite3.h')
sqlite_tmp_dir = os.path.join(sqlite_dir, sqlite_package)
sqlite_zip_file = sqlite_tmp_dir + '.zip'
sqlite_c_tmp = os.path.join(sqlite_tmp_dir, 'sqlite3.c')
sqlite_h_tmp = os.path.join(sqlite_tmp_dir, 'sqlite3.h')

sqlite_env = env.Clone()

if target_os in targets_need_sqlite:
    print('*** Checking for presence of %s ***' % sqlite_package)
    os.listdir(sqlite_dir)
    if not os.path.exists(sqlite_c) or not os.path.exists(sqlite_h):
        if not os.path.exists(sqlite_zip_file):
            sqlite_zip_file = sqlite_env.Download(sqlite_zip_file, sqlite_url)
        sqlite_env.UnpackAll(sqlite_tmp_dir, sqlite_zip_file)
        os.rename(sqlite_c_tmp, sqlite_c)
        os.rename(sqlite_h_tmp, sqlite_h)
        os.remove(sqlite_zip_file)
        shutil.rmtree(sqlite_tmp_dir)

if target_os in ['windows']:
    if sqlite_env.get('MSVC_UWP_APP') == '1':
        sqlite_env.AppendUnique(CPPDEFINES=['SQLITE_OS_WINRT'])
    libsqlite3 = sqlite_env.StaticLibrary(build_dir + 'sqlite3', sqlite_c)
    sqlite_env.UserInstallTargetLib(libsqlite3)
